package com.sky.controller.category;

import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.pojo.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@Slf4j
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
@RequiredArgsConstructor
public class SetmealController {


    private final SetmealService setmealService;

    /**
     * 分页查询套餐
     */
    @ApiOperation("分页查询套餐")
    @GetMapping("/page")
    public Result pageQuery(SetmealPageQueryDTO dto) {
        // log.info("接收到的参数为:{}",dto);
        PageResult pageResult = setmealService.pageQuery(dto);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     */
    @PostMapping
    @ApiOperation("新增|修改套餐")
    public Result save(@RequestBody @Validated SetmealDTO dto) {

        setmealService.save(dto);

        return Result.success();

    }

    /**
     * 修改套餐
     * @param dto
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated SetmealDTO dto) {

        setmealService.save(dto);

        return Result.success();

    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping
    @ApiOperation("根据id批量删除")
    public Result delByIds(@RequestParam List<Long> ids) {
        try {
            setmealService.delByIds(ids);
        } catch (Exception e) {
            log.info("删除失败{}",e);
            return Result.error("删除套餐中存在在售状态套餐,请停售重新删除");
        }
        return Result.success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{setmealId}")
    @ApiOperation("查询回显")
    public Result queryById(@PathVariable Long setmealId) {
        SetmealVO vo = setmealService.queryById(setmealId);
        return Result.success(vo);
    }

    /**
     * 修改套餐状态
     */
    @PostMapping("/status/{status}")
    public Result switchStatus(@PathVariable Integer status ,Long id){
        log.info("接收到的参数为:{},{}",status,id);
        SetmealDTO dto = new SetmealDTO();
        dto.setStatus(status);
        dto.setId(id);
        try {
            setmealService.save(dto);
        } catch (Exception e) {
            log.info("{}",e);
            return Result.error(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        return Result.success();
    }
}
